package element.synt;

import ast.AstItem;
import ast.ComplexDataSignature;
import ast.Expression;
import element.base.Synt;
import element.base.Word;
import element.base.WordType;

public class UnaryExp extends Synt {
    @Override
    public AstItem toAst() {
        if (getNodes().getFirst().isSynt(UnaryOp.class)) {
            Word op = findFirst();
            UnaryExp unaryExp = (UnaryExp) getNodes().getLast();
            AstItem unaryAst = unaryExp.toAst();
            Expression expression = new Expression();
            expression.operands.add(unaryAst);
            if (op.isWord(WordType.ADD)) {
                expression.operator = Expression.OperatorType.OP_ADD;
            } else if (op.isWord(WordType.MINU)) {
                expression.operator = Expression.OperatorType.OP_MINU;
            } else {
                expression.operator = Expression.OperatorType.OP_NOT;
            }
            return expression;
        } else if (getNodes().getFirst().isSynt(PrimaryExp.class)) {
            return ((PrimaryExp) getNodes().getFirst()).toAst();
        } else {
            return ((ComplexUnaryExp) getNodes().getFirst()).toAst();
        }
    }
}
